package com.human.server.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.human.server.entity.Announcements;
import com.human.server.enumration.AnnouncementStatus;
import com.human.server.model.R;
import com.human.server.model.request.AnnouncementsRequest;
import com.human.server.service.impl.AnnouncementsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 通知管理
 */
@RestController
@RequestMapping("/human/announcements")
public class AnnouncementsController {

    @Autowired
    private AnnouncementsServiceImpl announcementsService;

    /**
     * 分页查询
     * @param announcementsRequest
     * @return
     */
    @PostMapping("/page")
    public R page(@RequestBody AnnouncementsRequest announcementsRequest) {
        return R.success(announcementsService.page(announcementsRequest, new LambdaQueryWrapper<Announcements>()
                .eq(StrUtil.isNotEmpty(announcementsRequest.getTitle()), Announcements::getTitle, announcementsRequest.getTitle())
                .eq(StrUtil.isNotEmpty(announcementsRequest.getType()), Announcements::getType, announcementsRequest.getType())
                .eq(StrUtil.isNotEmpty(announcementsRequest.getStatus()), Announcements::getStatus, announcementsRequest.getStatus())
        ));
    }

    /**
     * 新增
     * @param announcements
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody Announcements announcements) {

        announcementsService.save(announcements);
        return R.success();
    }

    /**
     * 修改
     * @param announcements
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody Announcements announcements) {

        announcementsService.updateById(announcements);
        return R.success();
    }

    /**
     * 删除
     * @param announcements
     * @return
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Announcements announcements) {

        announcementsService.removeById(announcements);
        return R.success();
    }

    /**
     * 详情
     * @param announcements
     * @return
     */
    @PostMapping("/detail")
    public R detail(@RequestBody Announcements announcements) {

        return R.success(announcementsService.getById(announcements.getId()));
    }


    /**
     * 列表
     * @param announcements
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody Announcements announcements) {

        return R.success(announcementsService.list(new LambdaQueryWrapper<Announcements>()
                .eq(Announcements::getStatus, announcements.getStatus())
                .orderByDesc(Announcements::getPublishTime)
        ));
    }

    /**
     * 归档
     */
    @PostMapping("/archive")
    public R archive(@RequestBody Announcements announcements) {

        announcementsService.update(new LambdaUpdateWrapper<Announcements>()
                .set(Announcements::getStatus, AnnouncementStatus.ARCHIVED.getValue())
                .eq(Announcements::getId, announcements.getId())
        );
        return R.success();
    }
}
